<template>
  <div>

    <div v-if="tryStatus">
      <div class="rankingBackView">
        <div class="headerView">
          <div>
            <p class="nameLabel">排行榜</p>
            <p class="tagLabel">距离NO.1你还差一丢丢努力</p>
          </div>
          <img class="imgView" src="/web-hft-ee/static/img/m/ranking_list_trophy.png">
        </div>
        <div class="listView">
          <ranking-unit-view></ranking-unit-view>
          <div v-for="(el, idx) in countList" style="padding-bottom: 16px">
            <ranking-cell :ranking-index="idx" :data="el"></ranking-cell>
          </div>
        </div>
        <div style="height: 32px"></div>
        <level-cover :show="tryStatus"></level-cover>
      </div>
    </div>
    <div v-else>

      <loading-view :data="rankingList">
        <div>
          <div class="rankingBackView">
            <div class="headerView">
              <div>
                <p class="nameLabel">排行榜</p>
                <p class="tagLabel">距离NO.1你还差一丢丢努力</p>
              </div>
              <img class="imgView" src="/web-hft-ee/static/img/m/ranking_list_trophy.png">
            </div>
            <div class="listView">
              <ranking-unit-view></ranking-unit-view>
              <van-list v-model="loading"
                        :offset="offset"
                        :finished="finished"
                        @load="getChartData(false)">
                <div v-for="(el, idx) in countList" style="padding-bottom: 16px">
                  <ranking-cell :ranking-index="idx" :data="el"></ranking-cell>
                </div>
              </van-list>

            </div>
            <div style="height: 32px"></div>
          </div>
        </div>

      </loading-view>

    </div>


  </div>

</template>

<script>

  import UniversityTool from '@/pages/university/UniversityTool'
  import RankingUnitView from './components/RankingUnitView'
  import RankingCell from './components/RankingCell'
  import LevelCover from '../components/LevelCover'
  import LoadingView from '@/components/LoadingView'
  import {Toast} from 'vant'

  export default {
    name: "ranking",
    props: {
      teamId: {
        type: Number,
        default: 0,
      },
      tryStatus: {
        type: Boolean,
        default: false,
      }
    },
    watch: {
      teamId: function (val) {
        Toast.loading({
          duration: 0,
          message: '加载中...',
          forbidClick: true,
        });
        this.getChartData(true)
      }
    },
    data() {
      return {
        rankingList: null,
        nextPage: 1,///下一次请求的页码
        loading: false,//数据加载中
        finished: false,///所有数据加载完成，没有更多
        offset: 10,
      }
    },
    components: {
      RankingCell,
      RankingUnitView,
      LevelCover,
      LoadingView
    },
    computed: {

      countList() {

        if (this.tryStatus) {
          return [
            {"nickname": "张三", "pic": "/web-hft-ee/static/img/university/demo_0.jpg", "learnValue": 1220},
            {"nickname": "李四", "pic": "/web-hft-ee/static/img/university/demo_1.jpg", "learnValue": 1092},
            {"nickname": "王五", "pic": "/web-hft-ee/static/img/university/demo_2.jpg", "learnValue": 908},
            {"nickname": "赵六", "pic": "/web-hft-ee/static/img/university/demo_3.jpg", "learnValue": 899},
            {"nickname": "孙七", "pic": "/web-hft-ee/static/img/university/demo_4.jpg", "learnValue": 777},
            {"nickname": "周八", "pic": "/web-hft-ee/static/img/university/demo_5.jpg", "learnValue": 620},
            {"nickname": "吴九", "pic": "/web-hft-ee/static/img/university/demo_6.jpg", "learnValue": 500},
            {"nickname": "赵十", "pic": "/web-hft-ee/static/img/university/demo_7.jpg", "learnValue": 469},
            {"nickname": "刘一", "pic": "/web-hft-ee/static/img/university/demo_8.jpg", "learnValue": 320},
            {"nickname": "陈二", "pic": "/web-hft-ee/static/img/university/demo_9.jpg", "learnValue": 9},
          ]
        } else {
          return this.rankingList
        }


      },
    },
    mounted() {
      if (this.teamId) {
        this.getChartData(true)
      }
    },
    methods: {
      getChartData(isRefresh) {

        this.loading = true
        if (isRefresh) {
          this.nextPage = 1
        }
        UniversityTool.getRankingData(this.teamId, 0, this.nextPage).then((list) => {

          this.nextPage += 1
          this.finished = list.length === 0
          if (isRefresh) {
            this.rankingList = list
          } else {
            this.rankingList = this.rankingList.concat(list)
          }

        }).finally(() => {
          Toast.clear()
          this.loading = false
        })
      }

    }

  }
</script>

<style scoped>
  .rankingBackView {
    min-height: 100vh;
    background-image: linear-gradient(to bottom, #FFD272, #FFA554);
  }

  .headerView {
    padding: 8px 0 0 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .nameLabel {
    color: white;
    font-size: 26px;
    font-weight: bold;

  }

  .tagLabel {
    font-size: 14px;
    color: white;
    margin-top: 8px;

  }

  .imgView {
    width: 50%;
    object-fit: cover;
  }

  .listView {
    margin: 16px 16px 16px 16px;
    padding: 16px 16px 0 16px;
    background-color: white;
    border-radius: 12px;

  }
</style>
